﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 系统通用数据检查规则
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //string strcon = "";
        DataTable dtM = new DataTable();
        #region 数据库操作

        private void GetDataTable(DataTable dt, string sql)
        {
            using (SqlDataAdapter da = new SqlDataAdapter(sql, Cpublic.strConn))
            {
                try
                {
                    da.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void SetDataTable(DataTable dt, string sql)
        {
            using (SqlDataAdapter da = new SqlDataAdapter(sql, Cpublic.strConn))
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(da);
                cb.ConflictOption = ConflictOption.OverwriteChanges;
                try
                {
                    da.Update(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        #endregion
        void fun_刷新()
        {
            dtM = new DataTable();
            GetDataTable(dtM,$"select * from {barEditItem4.EditValue}");
            gridView1.Columns.Clear();
            gridControl1.DataSource = dtM;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            fun_刷新();
            // gridControl1.DataSource = dtM;
        }

        private void barLargeButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                fun_刷新();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void barLargeButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            try
            {
                DataRow dr = dtM.NewRow();
                dtM.Rows.Add(dr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void barLargeButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataRow r = (this.BindingContext[dtM].Current as DataRowView).Row;
                r.Delete();
                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void barLargeButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                gridView1.CloseEditor();
                (this.BindingContext[dtM] as CurrencyManager).EndCurrentEdit();
                SetDataTable(dtM, $"select * from {barEditItem4.EditValue} where 1<>1");
                fun_刷新();
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
